package com.by.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@EnableAspectJAutoProxy(proxyTargetClass = true)
@Aspect
@Component
public class AopLog {

    @Around("@annotation(needTest)")
    public Object test(ProceedingJoinPoint joinPoint, NeedTest needTest)throws Throwable {

        Long startTime = System.currentTimeMillis();

        String classname = joinPoint.getTarget().getClass().getName();
        String method = joinPoint.getSignature().getName();

        Object object = null;

        try {
            object = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            throw throwable;
        }
        Long endTime = System.currentTimeMillis();

        System.out.println(classname + " " + method +" "+ object +" " +(endTime - startTime));

        return object;

    }

}
